package com.solweaver.greetings.utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.solweaver.greetings.dto.BaseResponse;
import com.solweaver.greetings.dto.ErrorDetail;
import com.solweaver.greetings.dto.EventCreationRequest;
import com.solweaver.greetings.dto.EventDTO;
import com.solweaver.greetings.dto.GenericEnum;
import com.solweaver.greetings.dto.EventErrorEnum;
import com.solweaver.greetings.dto.UserDTO;
import com.solweaver.greetings.dto.UserMetaDataDTO;
import com.solweaver.greetings.entity.Event;
import com.solweaver.greetings.entity.EventStatus;
import com.solweaver.greetings.entity.Invitee;
import com.solweaver.greetings.entity.InviteStatus;
import com.solweaver.greetings.entity.User;
import com.solweaver.greetings.entity.UserEventType;
import com.solweaver.greetings.entity.UserMetaData;
import com.solweaver.greetings.entity.UserStatus;

public class GenericUtils {

	public static void buildErrorDetail(BaseResponse baseResponse, EventErrorEnum inviteErrorEnum){
		List<ErrorDetail> errorDetailList = baseResponse.getErrorDetailList();
		if(errorDetailList == null){
			errorDetailList = new ArrayList<ErrorDetail>();
		}
		ErrorDetail errorDetail = new ErrorDetail();
		errorDetail.setCode(inviteErrorEnum.code);
		errorDetail.setMessage(inviteErrorEnum.message);
		errorDetailList.add(errorDetail);
		baseResponse.setErrorDetailList(errorDetailList);
	}
	
	public static void buildErrorDetail(BaseResponse baseResponse, GenericEnum genericEnum){
		
		List<ErrorDetail> errorDetailList = baseResponse.getErrorDetailList();
		if(errorDetailList == null){
			errorDetailList = new ArrayList<ErrorDetail>();
		}
		ErrorDetail errorDetail = new ErrorDetail();
		errorDetail.setCode(genericEnum.code);
		errorDetail.setMessage(genericEnum.message);
		errorDetailList.add(errorDetail);
		baseResponse.setErrorDetailList(errorDetailList);
	}
	
	public static boolean isSuccess(List<ErrorDetail> errorDetailList){
		boolean result = false;
		if(errorDetailList == null || (errorDetailList.size() == 1 && errorDetailList.get(0).getCode().equals(GenericEnum.Success.code))){
			result = true;
		}
		return result;
	}

	public static Event convertToEventEntity(
			EventCreationRequest eventCreationRequest) {
		Event event = new Event();
		event.setCreatedBy(convertToUserEntity(eventCreationRequest.getCreatedByUser()));
		event.setDescription(eventCreationRequest.getDescription());
		event.setEventName(eventCreationRequest.getEventName());
		event.setEnableReminder(eventCreationRequest.isEnableReminder());
		event.setFromMessage(eventCreationRequest.getFromMessage());
		try{
			//event.setEventDate(GenericUtils.converToDate(eventCreationRequest.getEventDate()));
			event.setEventDate(eventCreationRequest.getEventDate());
			event.setEventLockDate(GenericUtils.converToDate(eventCreationRequest.getEventLockDate()));
		}catch(Exception exception){
			exception.printStackTrace();
		}
		event.setEventStatus(EventStatus.Active);
		if(eventCreationRequest.getInviteeList() != null && eventCreationRequest.getInviteeList().size() > 0){
			List<Invitee> inviteeList = new ArrayList<Invitee>();
			for(UserDTO userDTO : eventCreationRequest.getInviteeList()){
				Invitee invitee = new Invitee();
				invitee.setEvent(event);
				invitee.setInviteeStatus(InviteStatus.Pending);
				invitee.setUser(convertToUserEntity(userDTO));
				inviteeList.add(invitee);
			}
			//event.setInviteeList(inviteeList);
		}
		return event;
	}
	
	public static User convertToUserEntity(UserDTO userDTO){
		User user = null;
		if(userDTO != null){
			user = new User();
			user.setId(userDTO.getUserId());
			user.setEmail(userDTO.getEmailId());
			user.setFirstName(userDTO.getFirstName());
			user.setLastName(userDTO.getLastName());
			user.setMobileNumber(userDTO.getMobileNumber());
			if(userDTO.getUserStatus() != null){
				user.setUserStatus(UserStatus.valueOf(userDTO.getUserStatus()));
			}
		}
		
		return user;
	}

	public static UserMetaData convertToUserMetaDataEntity(
			UserMetaDataDTO userMetaDataDTO) {
		UserMetaData userMetaData = null;
		
		if(userMetaDataDTO != null){
			userMetaData = new UserMetaData();
			userMetaData.setLastLoggedInDeviceId(userMetaDataDTO.getLastLoggedInDeviceId());
			userMetaData.setLastLoggedInTime(userMetaDataDTO.getLastLoggedInTime());
			userMetaData.setProviderId(userMetaDataDTO.getProviderId());
			userMetaData.setRegisteredDeviceId(userMetaDataDTO.getRegisteredDeviceId());
			if(userMetaDataDTO.getUserId() != null){
				User user = new User();
				user.setId(userMetaDataDTO.getUserId());
				userMetaData.setUser(user);
			}
			if(userMetaDataDTO.getUserMetaDataId() != null){
				userMetaData.setId(userMetaDataDTO.getUserMetaDataId());
			}
		}
		return userMetaData;
	}

	
	
	
	public static Date converToDate(String date) throws Exception{
		
		
		/*Date eventDate = null;
		if(date != null){
			SimpleDateFormat dateFormat = new SimpleDateFormat("mm-dd-yyyy");
			try{
				eventDate = dateFormat.parse(date);
			}catch(Exception exception){
				exception.printStackTrace();
				throw exception;
			}
		}
		return eventDate;*/
		
		String epochString = date;
		long epoch = Long.parseLong( epochString );
		Date expiry = new Date( epoch );
		//String targetDate = expiry.toString();
		
		Date eventDate = null;
		if(date != null){
			//SimpleDateFormat dateFormat = new SimpleDateFormat("mm-dd-yyyy");
			try{
				eventDate = expiry;//dateFormat.parse(targetDate); //expiry;
				}catch(Exception exception){
				exception.printStackTrace();
				throw exception;
			}
		}
		return eventDate;
	}

	public static Invitee convertToInviteeEntity(
			UserDTO userDTO, Event event, User user) {
		Invitee invitee = null;
		if(userDTO != null){
			invitee = new Invitee();
			invitee.setEmail(userDTO.getEmailId());
			invitee.setInviteeStatus(InviteStatus.Pending);
			invitee.setEvent(event);
			if(user != null){
				invitee.setUser(user);
			}
		}
		return invitee;
	}

	public static List<EventDTO> convertToEventDTOList(List<Event> eventList) {
		List<EventDTO> eventDTOList = null;
		
		if(eventList != null && eventList.size() > 0){
			eventDTOList = new ArrayList<EventDTO>();
			for(Event event : eventList){
				eventDTOList.add(convertToEventDTO(event));
			}
		}
		return eventDTOList;
	}

	public static EventDTO convertToEventDTO(Event event) {
		EventDTO eventDTO = null;
		if(event != null){
			eventDTO = new EventDTO();
			eventDTO.setCreatedByUserId(event.getCreatedBy().getId());
			eventDTO.setDescription(event.getDescription());
			eventDTO.setEventDate(event.getEventDate());
			eventDTO.setEventId(event.getId());
			eventDTO.setEventLockDate(event.getEventLockDate());
			eventDTO.setEventStatus(event.getEventStatus());
			eventDTO.setUserEventType(UserEventType.Event);
			eventDTO.setEventName(event.getEventName());
			eventDTO.setEnableReminder(event.isEnableReminder());
			if(event.getRecepientCountry() != null){
				eventDTO.setRecipientCountry(event.getRecepientCountry().getCode());
			}
		}
		return eventDTO;
	}

	public static UserDTO convertToUserDTO(User user) {
		UserDTO userDTO = null;
		if(user != null){
			userDTO = new UserDTO();
			userDTO.setEmailId(user.getEmail());
			userDTO.setFirstName(user.getFirstName());
			userDTO.setLastName(user.getLastName());
			userDTO.setMobileNumber(user.getMobileNumber());
			userDTO.setUserId(user.getId());
			userDTO.setUserStatus(user.getUserStatus().name());
		}
		return userDTO;
	}
	
}
